package v1

import (
	"ginTea/model"
	"ginTea/utils/errmsg"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

// 添加公告
func AddNotice(c *gin.Context) {
	var code int
	var data model.Notice
	_ = c.ShouldBindJSON(&data)
	code = model.CreateNotice(&data)
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"success": true,
	})
}

// 删除公告
func DeleteNotice(c *gin.Context) {
	var code int
	id, _ := strconv.Atoi(c.Param("id"))
	code = model.DeleteNotice(id)
	if code == errmsg.ERROR {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": false,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": true,
		})
	}
}

// 获取公告列表
func GetNoticeList(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pageSize"))
	page, _ := strconv.Atoi(c.Query("page"))

	if pageSize == 0 {
		pageSize = 10
	}
	if page == 0 {
		page = 1
	}
	data := model.GetNoticeList(page, pageSize)
	c.JSON(http.StatusOK, data)
}

// 获取公告详情
func GetNoticeInfo(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	data, code := model.GetNoticeInfo(id)
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"data":    data[0],
		"msg":     errmsg.GetErrMsg(code),
		"success": true,
	})
}

// 编辑公告
func UpdateNotice(c *gin.Context) {
	var code int
	var data model.Notice
	id, _ := strconv.Atoi(c.Param("id"))
	c.ShouldBindJSON(&data)
	code = model.UpdateNotice(id, &data)
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"success": true,
	})
}
